/**
 * Copyright 2010 upTick Pty Ltd
 *
 * Licensed under the terms of the GNU Lesser General Public License version 3 
 * as published by the Free Software Foundation. You may obtain a copy of the
 * License at: http://www.gnu.org/copyleft/lesser.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations 
 * under the License. 
 */

package au.com.uptick.serendipity.client.data;

import com.google.gwt.i18n.client.LocaleInfo;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.FieldType;

public class SalesDataSource extends DataSource {

    private static final String DATA_SOURCE = "salesDS";
    private static final String ICON = "icon";
    private static final String ICON_DISPLAY_NAME = "Icon";
    private static final String NAME = "name";
    private static final String NAME_DISPLAY_NAME = "Name";
    private static final String RECORD_XPATH = "/list/record";
    private static final String URL_PREFIX = "datasource/data/salesdatasource";
    private static final String URL_SUFFIX = ".xml";

    private static SalesDataSource instance = null;

    public static SalesDataSource getInstance() {
        if (instance == null) {
            instance = new SalesDataSource(DATA_SOURCE);
        }
        return instance;
    }

    public SalesDataSource(String id) {
        setID(id);
        setDataFormat(DSDataFormat.XML);
        setRecordXPath(RECORD_XPATH);
        DataSourceField iconField = new DataSourceField(ICON, FieldType.TEXT, ICON_DISPLAY_NAME);
        DataSourceField nameField = new DataSourceField(NAME, FieldType.TEXT, NAME_DISPLAY_NAME);
        setFields(iconField, nameField);

        String url = URL_PREFIX;
        LocaleInfo localeInfo = LocaleInfo.getCurrentLocale();
        String localeName = localeInfo.getLocaleName();

        if (localeName.length() > 0) {
            url = url + "_" + localeName;
        }

        url = url + URL_SUFFIX;
        setDataURL(url);
    }
}
